﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using ItemQuoteLibrary;

namespace TcpSendComplexMessages_Client
{
    class Program
    {
        private static void Main(string[] args)
        {
            string serverAddress = "192.168.1.2";
            int communicationPort = 7;
            ItemQuote itemMessage = new ItemQuote(1234567890987654L, "5mm Super Widgets", 1000, 12999, true, false);
            ItemQuoteEncoder itemEncoder = new ItemQuoteEncoderBin();

            TcpClient client = new TcpClient(serverAddress, communicationPort);
            NetworkStream stream = client.GetStream();

            byte[] byteMessage = itemEncoder.Encode(itemMessage);

            stream.Write(byteMessage, 0, byteMessage.Length);

            Console.WriteLine("Sending Text-Encoded Quote (" + byteMessage.Length + " bytes): ");
            Console.WriteLine(itemMessage);

            stream.Write(byteMessage, 0, byteMessage.Length);

            ItemQuoteDecoder decoder = new ItemQuoteDecoderBin();
            ItemQuote receivedItemMessage = decoder.Decode(client.GetStream());
            Console.WriteLine("Received Binary-Encode Quote:");
            Console.WriteLine(receivedItemMessage);

            stream.Close();
            client.Close();
        }
    }
}
